package com.chenenjun.db;

import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSourceFactory;

public class DruidDB {
	
	private static Properties pro = null;
	static{
		pro = new Properties();
		InputStream is = DruidDB.class.getClassLoader().getResourceAsStream("druid.ini");
		try {
			pro.load(is);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	/**
	 * 取得数据库连接对象
	 * @return
	 */
	public static Connection getConnection(){
		DataSource ds = null;
		Connection conn = null;
		try {
			ds = DruidDataSourceFactory.createDataSource(pro);
			conn = ds.getConnection();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	
	public static void main(String[] args) {
		if(getConnection() != null){
			System.out.println("数据库连接成功！");
		}else{
			System.out.println("数据库连接失败！");
		}
	}
}
